package com.sjgs.gis.service;

import java.util.List;

import javax.persistence.criteria.JoinType;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import io.github.jhipster.service.QueryService;

import com.sjgs.gis.domain.NodePlugin;
import com.sjgs.gis.domain.*; // for static metamodels
import com.sjgs.gis.repository.NodePluginRepository;
import com.sjgs.gis.service.dto.NodePluginCriteria;
import com.sjgs.gis.service.dto.NodePluginDTO;
import com.sjgs.gis.service.mapper.NodePluginMapper;

/**
 * Service for executing complex queries for {@link NodePlugin} entities in the database.
 * The main input is a {@link NodePluginCriteria} which gets converted to {@link Specification},
 * in a way that all the filters must apply.
 * It returns a {@link List} of {@link NodePluginDTO} or a {@link Page} of {@link NodePluginDTO} which fulfills the criteria.
 */
@Service
@Transactional(readOnly = true)
public class NodePluginQueryService extends QueryService<NodePlugin> {

    private final Logger log = LoggerFactory.getLogger(NodePluginQueryService.class);

    private final NodePluginRepository nodePluginRepository;

    private final NodePluginMapper nodePluginMapper;

    public NodePluginQueryService(NodePluginRepository nodePluginRepository, NodePluginMapper nodePluginMapper) {
        this.nodePluginRepository = nodePluginRepository;
        this.nodePluginMapper = nodePluginMapper;
    }

    /**
     * Return a {@link List} of {@link NodePluginDTO} which matches the criteria from the database.
     * @param criteria The object which holds all the filters, which the entities should match.
     * @return the matching entities.
     */
    @Transactional(readOnly = true)
    public List<NodePluginDTO> findByCriteria(NodePluginCriteria criteria) {
        log.debug("find by criteria : {}", criteria);
        final Specification<NodePlugin> specification = createSpecification(criteria);
        return nodePluginMapper.toDto(nodePluginRepository.findAll(specification));
    }

    /**
     * Return a {@link Page} of {@link NodePluginDTO} which matches the criteria from the database.
     * @param criteria The object which holds all the filters, which the entities should match.
     * @param page The page, which should be returned.
     * @return the matching entities.
     */
    @Transactional(readOnly = true)
    public Page<NodePluginDTO> findByCriteria(NodePluginCriteria criteria, Pageable page) {
        log.debug("find by criteria : {}, page: {}", criteria, page);
        final Specification<NodePlugin> specification = createSpecification(criteria);
        return nodePluginRepository.findAll(specification, page)
            .map(nodePluginMapper::toDto);
    }

    /**
     * Return the number of matching entities in the database.
     * @param criteria The object which holds all the filters, which the entities should match.
     * @return the number of matching entities.
     */
    @Transactional(readOnly = true)
    public long countByCriteria(NodePluginCriteria criteria) {
        log.debug("count by criteria : {}", criteria);
        final Specification<NodePlugin> specification = createSpecification(criteria);
        return nodePluginRepository.count(specification);
    }

    /**
     * Function to convert {@link NodePluginCriteria} to a {@link Specification}
     * @param criteria The object which holds all the filters, which the entities should match.
     * @return the matching {@link Specification} of the entity.
     */
    protected Specification<NodePlugin> createSpecification(NodePluginCriteria criteria) {
        Specification<NodePlugin> specification = Specification.where(null);
        if (criteria != null) {
            if (criteria.getId() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getId(), NodePlugin_.id));
            }
            if (criteria.getPluginId() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getPluginId(), NodePlugin_.pluginId));
            }
            if (criteria.getData() != null) {
                specification = specification.and(buildStringSpecification(criteria.getData(), NodePlugin_.data));
            }
            if (criteria.getSql() != null) {
                specification = specification.and(buildStringSpecification(criteria.getSql(), NodePlugin_.sql));
            }
            if (criteria.getInstallTime() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getInstallTime(), NodePlugin_.installTime));
            }
            if (criteria.getStatus() != null) {
                specification = specification.and(buildSpecification(criteria.getStatus(), NodePlugin_.status));
            }
            if (criteria.getNodeId() != null) {
                specification = specification.and(buildSpecification(criteria.getNodeId(),
                    root -> root.join(NodePlugin_.node, JoinType.LEFT).get(Node_.id)));
            }
        }
        return specification;
    }
}
